<template>
  <div>
    <div style="background-color: #f8f8f8; padding: 30px">
      <div style="width: 70%; margin: 0 auto">
        <el-input prefix-icon="Search" clearable @keyup.enter="loadSongs" @clear="loadSongs" v-model="data.name" placeholder="请输入歌曲名称查询" style="width: 400px; height: 40px"></el-input>
      </div>
    </div>
    <div style="width: 70%; margin: 30px auto">
      <div style="margin-bottom: 40px; display: flex; align-items: center; grid-gap: 30px">
        <div style="font-size: 18px">歌曲类型</div>
        <div>
          <el-button :type="checkCategory(null)" @click="changeCategory(null)">全部</el-button>
          <el-button :type="checkCategory(item.id)" @click="changeCategory(item.id)" v-for="item in data.categoryData" :key="item.id">{{ item.name }}</el-button>
        </div>
      </div>
      <el-table :data="data.songList">
        <el-table-column prop="name" label="名称" width="200" show-overflow-tooltip>
          <template v-slot="scope">
            <a :href="'/front/songDetail?id=' + scope.row.id" style="color: #1357be">{{ scope.row.name }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="composer" label="作曲人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="lyricist" label="作词人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="singerName" label="歌手" width="100">
          <template v-slot="scope">
            <img :src="scope.row.singerAvatar" alt="" @click="router.push('/front/singerDetail?id=' + scope.row.singerId)" style="width: 30px; height: 30px; border-radius: 50%; display: block; cursor: pointer">
          </template>
        </el-table-column>
        <el-table-column prop="time" label="发行时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="albumName" label="所属专辑" show-overflow-tooltip>
          <template v-slot="scope">
            <a :href="'/front/albumDetail?id=' + scope.row.albumId + '&singerId=' + scope.row.singerId" style="color: #1357be">《{{ scope.row.albumName }}》</a>
          </template>
        </el-table-column>
        <el-table-column prop="categoryName" label="所属分类" show-overflow-tooltip></el-table-column>
        <el-table-column prop="duration" label="时长" show-overflow-tooltip></el-table-column>
        <el-table-column prop="hot" label="热度">
          <template v-slot="scope">🔥{{ scope.row.hot }}</template>
        </el-table-column>
        <el-table-column label="" width="100" fixed="right">
          <template v-slot="scope">
            <div style="display: flex; align-items: center; grid-gap: 10px">
              <el-icon style="cursor: pointer; color: #8c8b8b" size="20" v-if="!scope.row.collectId" @click="collectSong(scope.row.id)"><Star /></el-icon>
              <el-icon style="cursor: pointer; color: #e1bc43" size="20" v-else @click="disCollectSong(scope.row.collectId)"><StarFilled /></el-icon>
              <el-icon style="cursor: pointer; color: #8c8b8b" size="20" @click="addPlaylistInit(scope.row.id)"><FolderAdd /></el-icon>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="data.total" style="margin-top: 20px">
        <el-pagination @current-change="loadSongs" layout="total,prev, pager, next" :page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
      </div>
    </div>
    <el-dialog title="我的歌单" v-model="data.formVisible" width="40%" destroy-on-close>
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="80px" style="padding: 20px">
        <el-form-item prop="playlistId" label="选择歌单">
          <el-select v-model="data.form.playlistId" placeholder="请选择歌单" style="width: 90%">
            <el-option v-for="item in data.playlistData" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const formRef = ref()

const data = reactive({
  user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
  categoryData: [],
  songList: [],
  total: 0,
  pageNum: 1,
  pageSize: 10,
  name: null,
  categoryId: null,
  playlistData: [],
  form: {},
  formVisible: false,
  rules: {
    playlistId: [
      { required: true, message: '请选择歌单', trigger: 'blur' }
    ],
  }
})

const addPlaylistInit = (songId) => {
  request.get('/playlist/selectAll', {
    params: {
      userId: data.user.id
    }
  }).then(res => {
    if (res.code === '200') {
      data.playlistData = res.data
      data.form = {}
      data.form.songId = songId
      data.formVisible = true
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const save = () => {
  formRef.value.validate(valid => {
    if (valid) {
      request.post('/playlistItem/add', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success('加入成功')
          data.formVisible = false
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

const loadSongs = () => {
  request.get('/sing/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name,
      categoryId: data.categoryId,
    }
  }).then(res => {
    if (res.code === '200') {
      data.songList = res.data?.list
      data.total = res.data?.total
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadSongs()

const loadCategory = () => {
  request.get('/category/selectAll').then(res => {
    if (res.code === '200') {
      data.categoryData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}

loadCategory()

const checkCategory = (categoryId) => {
  return data.categoryId === categoryId ? 'primary' : ''
}

const changeCategory = (categoryId) => {
  data.categoryId = categoryId
  loadSongs()
}

const collectSong = (songId) => {
  request.post('/collect/add', {
    relId: songId,
    type: 'SONG'
  }).then(res => {
    if (res.code === '200') {
      ElMessage.success('收藏歌曲成功')
      loadSongs()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const disCollectSong = (id) => {
  request.delete('/collect/delete/' + id).then(res => {
    if (res.code === '200') {
      ElMessage.success('取消成功')
      loadSongs()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const reset = () => {
  data.name = null
  loadSongs()
}
</script>